from flask import Flask
from flask_migrate import Migrate
from .database import db  # 修正为相对导入

def create_app():
    app = Flask(__name__)
    
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tw_dashboard.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    db.init_app(app)
    
    # Import models after db is initialized
    with app.app_context():
        from .models.production import ProductionBatch
        from .models.user import User, Role, Permission
        # Import other models as needed
        
    Migrate(app, db)
    
    return app
